<template>
  <transition name="notification">
    <div v-if="value" class="detail-outsize-box" :style="{ 'z-index': zIndex }">
      <div
        class="detail-content"
        :style="{
          maxHeight: height + 'px',
          width: width + 'px',
          paddingBottom:
            isNeedFooter && filterBtnList(footerList).length > 0 ? 0 : `24px`,
        }"
      >
        <div class="detail-top">
          <div class="detail-left" :class="{ 'only-left': !rightList.length }">
            <div class="detail-head">
              <div class="detail-title">{{ popupTitle }}</div>
              <div
                v-if="statusModel != 'hidden'"
                class="detail-status"
                :style="{ 'margin-right': rightList.length < 1 ? '40px' : 0 }"
              >
                <div class="enable-status" v-if="statusModel == 'enable'">
                  启停状态：<span class="status-label">启用</span>
                </div>
                <div
                  v-if="statusModel == 'deactivate'"
                  class="deactivate-status"
                >
                  启停状态：<span class="status-label">停用</span>
                </div>
                <div
                  v-if="statusModel == 'custom'"
                  class="custom-status"
                  :style="{
                    background: customStatus.background,
                  }"
                >
                  {{ customStatus.label }}：<span
                    class="status-label"
                    :style="{ color: customStatus.color }"
                    >{{ customStatus.value }}</span
                  >
                </div>
              </div>
              <div
                class="detail-close left-close"
                v-if="rightList.length < 1"
                @click="emitHandle('close')"
              ></div>
            </div>
            <div class="detail-wrap" ref="detailWrap">
              <div
                class="detail-item"
                v-for="(item, index) in leftList"
                :key="index"
                :class="[
                  {
                    bottom32: item.slotBottom == 32,
                  },
                  {
                    'text-status': item.type == 'text',
                  },
                  {
                    'image-status': item.type == 'image',
                  },
                ]"
              >
                <template v-if="item.label">
                  <div class="item-title">
                    <div class="title-icon"></div>
                    <div class="item-name">{{ item.label }}</div>
                  </div>
                </template>

                <div class="item-list">
                  <template v-if="item.type == 'text'">
                    <template v-for="(sItem, sIndex) in item.list">
                      <div
                        v-if="!sItem.hide"
                        class="list-wrap"
                        :key="sIndex"
                        :style="{ width: sItem.width + '%' }"
                      >
                        <div
                          class="list-label"
                          :style="{
                            color: sItem.labelColor,
                            width: widthLabel + 'px',
                          }"
                        >
                          {{ sItem.text }}
                        </div>
                        <div
                          class="list-value"
                          :style="{
                            color: sItem.valueColor,
                          }"
                        >
                          {{ detailData[sItem.id] }}
                        </div>
                      </div>
                    </template>
                  </template>
                  <template v-if="item.type == 'record'">
                    <TrRecord :recordList="item.list" />
                  </template>
                  <template v-if="item.type == 'table'">
                    <TrTable
                      isPopup
                      :isNeedIndex="item.isNeedIndex"
                      v-model="item.searchParams"
                      :total="item.total"
                      :tableHeight="item.height"
                      :tableData="item.tableData"
                      :isNeedPagination="item.isNeedPagination"
                      :tableColumn="item.tableColumn"
                      :requestName="item.requestName"
                    />
                  </template>
                  <template v-if="item.type == 'slot'">
                    <slot :name="item.slotName" v-bind="item"></slot>
                  </template>
                  <template v-if="item.type == 'image'">
                    <div class="list-wrap" :style="{ width: item.width + '%' }">
                      <div class="list-label">{{ item.fileLabel }}</div>
                      <div
                        class="list-value"
                        v-if="
                          (Array.isArray(detailData[item.id]) &&
                            detailData[item.id].length > 0) ||
                          (detailData[item.id] != '-' &&
                            !Array.isArray(detailData[item.id]))
                        "
                      >
                        <template v-if="Array.isArray(detailData[item.id])">
                          <div
                            class="image-wrap"
                            v-for="(mItem, mIndex) in detailData[item.id]"
                            :key="mIndex"
                          >
                            <el-image
                              class="el-image-item"
                              :src="mItem"
                              :preview-src-list="detailData[item.id]"
                            >
                              <div slot="error" class="image-slot">
                                <i class="el-icon-picture-outline"></i>
                              </div>
                            </el-image>
                          </div>
                        </template>
                        <template v-else>
                          <div class="image-wrap">
                            <el-image
                              class="el-image-item"
                              :src="detailData[item.id]"
                              :preview-src-list="[detailData[item.id]]"
                            >
                              <div slot="error" class="image-slot">
                                <i class="el-icon-picture-outline"></i>
                              </div>
                            </el-image>
                          </div>
                        </template>
                      </div>
                      <div class="list-value" v-else>-</div>
                    </div>
                  </template>
                </div>
              </div>
            </div>
          </div>
          <div class="detail-right" v-if="rightList.length > 0">
            <div class="detail-head">
              <div class="detail-title"></div>
              <div class="detail-close" @click="emitHandle('close')"></div>
            </div>
            <div class="detail-wrap">
              <div
                class="detail-item"
                v-for="(item, index) in rightList"
                :key="index"
              >
                <div class="item-title">
                  <div class="title-icon"></div>
                  <div class="item-name">{{ item.label }}</div>
                </div>
                <div class="item-list">
                  <template v-if="item.type == 'text' && !item.hide">
                    <template v-for="(sItem, sIndex) in item.list">
                      <div
                        v-if="!sItem.hide"
                        class="list-wrap"
                        :key="sIndex"
                        :style="{ width: sItem.width + '%' }"
                      >
                        <div
                          class="list-label"
                          :style="{
                            color: sItem.labelColor,
                          }"
                        >
                          {{ sItem.text }}:
                        </div>
                        <div
                          class="list-value"
                          :style="{
                            color: sItem.valueColor,
                          }"
                        >
                          {{ detailData[sItem.id] }}
                        </div>
                      </div>
                    </template>
                  </template>
                  <template v-if="item.type == 'record'">
                    <TrRecord :recordList="item.list" />
                  </template>
                  <template v-if="item.type == 'table'">
                    <TrTable
                      :tableHeight="item.height"
                      :tableData="item.tableData"
                      :isNeedPagination="item.isNeedPagination || false"
                      :recordList="item.list"
                      :tableColumn="item.tableColumn"
                    />
                  </template>
                  <template v-if="item.type == 'slot'">
                    <slot :name="item.slotName"></slot>
                  </template>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div
          class="footer-list"
          :class="{ 'has-scroll': checkIsScroll }"
          v-if="isNeedFooter && filterBtnList(footerList).length > 0"
        >
          <div
            class="footer-btn"
            :class="item.color"
            v-for="(item, index) in filterBtnList(footerList)"
            :key="index"
            @click="emitHandle(item.option)"
          >
            {{ item.label }}
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import { getStore } from "@/utils/store";
import TrRecord from "./operation-record.vue";
import TrTable from "../Tr-table/Tr-table.vue";
export default {
  name: "TrDetailBox",
  components: {
    TrRecord,
    TrTable,
  },

  props: {
    value: {
      type: Boolean,
      default: false,
    },
    popupTitle: {
      type: String,
      default: "查看详情",
    },
    customStatus: {
      type: String,
      default: () => {},
    },
    statusModel: {
      type: String,
      default: "hidden",
    },
    detailData: {
      type: Object,
      default: () => {},
    },
    leftList: {
      type: Array,
      default: () => [],
    },
    rightList: {
      type: Array,
      default: () => [],
    },
    zIndex: {
      type: Number,
      default: 1001,
    },
    height: {
      type: Number,
      default: 720,
    },
    labelWidth: {
      type: Number,
      default: 72,
    },
    width: {
      type: Number,
      default: 750,
    },
    isNeedFooter: {
      type: Boolean,
      default: false,
    },
    footerList: {
      type: Array,
      default: () => [
        {
          label: "取消",
          option: "cancel",
          color: "white",
        },
        {
          label: "保存",
          option: "confrim",
          color: "blue",
        },
      ],
    },
  },
  watch: {
    value: {
      handler(val) {
        if (!val) return;
        this.getLabelWidth();
        this.$nextTick(() => {
          this.observeWithResizeObserver();
        });
      },
    },
  },
  data: () => ({
    checkIsScroll: false,
    clientHeight: 0,
    widthLabel: 70,
    permissionObject: getStore({ name: "permission" }),
    userInfo: getStore({ name: "userInfo" }),
    routeName: "",
  }),
  methods: {
    filterBtnList(list) {
      if (this.userInfo.role_name.indexOf("admin") > -1)
        return list ? list : [];
      if (!list || !list.length || !this.permissionObject) return [];
      let perList = this.permissionObject[this.routeName];
      if (!perList) return [];
      let newBtnList =
        list.filter((item) => perList.includes(item.option)) || [];
      return newBtnList;
    },
    observeWithResizeObserver() {
      const element = this.$refs.detailWrap;
      const observer = new ResizeObserver((entries) => {
        entries.forEach((entry) => {
          this.checkIsScroll =
            entry.target.scrollHeight > Math.ceil(entry.contentRect.height);
        });
      });

      observer.observe(element);
      this.$once("hook:beforeDestroy", () => {
        observer.disconnect();
      });
    },
    emitHandle(type) {
      if (type == "close") {
        this.$emit("input", false);
      }
      this.$emit(type, type);
    },
    getLabelWidth() {
      const textList = this.leftList.filter((item) => item.type == "text");
      if (!textList.length) return;
      const maxTextLength = textList.reduce((maxLength, obj) => {
        const maxInList = obj.list.reduce((max, item) => {
          return Math.max(max, item.text.length);
        }, 0);

        return Math.max(maxLength, maxInList);
      }, 0);
      if (maxTextLength >= 8) {
        this.widthLabel = 9 * 14;
      } else {
        this.widthLabel = (maxTextLength + 1) * 14;
      }
    },
  },
  created() {
    this.routeName = this.$route.path.split("/").pop();
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-tabs {
  width: 100%;
  .el-tabs__header {
    margin: 0 0 24px;
    .el-tabs__item {
      height: 48px;
      line-height: 48px;
      &:hover {
        color: $main-color;
      }
      &.is-active {
        color: $main-color;
      }
      &.is-focus {
        box-shadow: none !important;
      }
    }
  }
  .el-tabs__nav-wrap::after {
    height: 1px;
    background-color: #e7e7e7;
  }
}
.detail-outsize-box {
  position: fixed;
  width: 100vw;
  height: 100vh;
  background: rgba(#000, 0.4);
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.5s;
  .detail-content {
    min-height: 200px;
    display: flex;
    flex-direction: column;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0px 2px 4px -1px rgba(0, 0, 0, 0.12),
      0px 4px 5px 0px rgba(0, 0, 0, 0.08), 0px 1px 10px 0px rgba(0, 0, 0, 0.05);
    overflow: hidden;
    .detail-top {
      padding: 0 24px;
      flex: 1;
      display: flex;
      overflow: hidden;
      .detail-head {
        position: relative;
        height: 28px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 16px;
        .detail-title {
          color: rgba(#000, 0.9);
          font-weight: 600;
          line-height: 24px;
        }
        .detail-status {
          min-width: 200px;
          text-align: right;
          color: rgba(#000, 0.9);
          font-size: 14px;
          font-weight: 400;
          line-height: 28px;
          .enable-status {
            padding-right: 24px;
            background: linear-gradient(
              270deg,
              rgba(0, 168, 112, 0.2) 0%,
              rgba(0, 168, 112, 0) 100%
            );
            .status-label {
              color: #00a870;
              font-weight: 600;
            }
          }
          .deactivate-status {
            padding-right: 24px;
            background: linear-gradient(
              270deg,
              rgba(227, 77, 89, 0.2) 0%,
              rgba(227, 77, 89, 0) 100%
            );
            .status-label {
              color: #e34d59;
              font-weight: 600;
            }
          }
          .custom-status {
            padding-right: 24px;
          }
        }
        .detail-close {
          width: 20px;
          height: 20px;
          background-image: url("~@/assets/icons/close_2.png");
          background-size: contain;
          cursor: pointer;
          &.left-close {
            position: absolute;
            right: 0;
            top: 6px;
          }
        }
      }
      .detail-left,
      .detail-right {
        display: flex;
        flex-direction: column;
        padding: 24px 0 0 0;
        width: 0;
        flex: 1;
      }
      .detail-left {
        flex: 5;
        padding: 24px 24px 0 0;
      }
      .only-left {
        padding: 24px 0 0 0;
        .detail-wrap {
          padding-right: 8px;
          margin-right: -8px;
        }
      }
      .detail-wrap {
        position: relative;
        flex: 1;
        padding-right: 6px;
        margin-right: -12px;
        margin-bottom: -32px;
        overflow-y: auto;
        overflow-x: hidden;
        .detail-item {
          margin-bottom: 32px;
          .item-title {
            position: relative;
            padding-left: 10px;
            margin-bottom: 16px;
            .title-icon {
              position: absolute;
              left: 0;
              top: 4px;
              width: 4px;
              height: 12px;
              border-radius: 4px;
              background: linear-gradient(
                180deg,
                rgba(107, 114, 255, 1) 0%,
                rgba(34, 39, 209, 1) 100%
              );
            }
            .item-name {
              color: rgba(#000, 0.9);
              font-weight: 600;
              font-size: 14px;
            }
          }
          .item-list {
            display: flex;
            flex-wrap: wrap;
            .list-wrap {
              display: flex;
              align-items: baseline;
              margin-bottom: 16px;
              .list-label {
                position: relative;
                padding-right: 14px;
                text-align: right;
                font-weight: 400;
                font-size: 14px;
                color: rgba(#000, 0.4);
                word-break: break-all;
                &::after {
                  position: absolute;
                  content: ":";
                  right: 8px;
                  top: -1px;
                  font-size: 14px;
                }
              }
              .list-value {
                padding-right: 32px;
                flex: 1;
                color: rgba(#000, 0.9);
                word-break: break-all;
                width: 0;
                font-size: 14px;
                font-weight: 400;
              }
              .image-wrap {
                height: 72px;
                width: 72px;
                display: flex;
                align-items: center;
                justify-content: center;
                font-size: 0;
                border-radius: 3px;
                border: 1px dashed #dcdcdc;
                background: #eeeeee;
                margin-right: 8px;
                margin-bottom: 8px;
                .el-image-item {
                  width: 64px;
                  height: 64px;
                }

                .el-icon-picture-outline {
                  font-size: 32px;
                  color: #dcdcdc;
                }
              }
            }
          }
          &:last-child {
            margin-bottom: 16px;
          }
          &.bottom32 {
            margin-bottom: 32px !important;
          }
          &.text-status {
            margin-bottom: 16px;
            margin-right: -32px;
          }
          &.image-status {
            .list-wrap {
              align-items: flex-start;
            }
            .list-value {
              display: flex;
              flex-wrap: wrap;
              margin-bottom: -8px;
            }
          }
        }
      }
      .detail-right {
        flex: 3;
        padding-left: 24px;
        border-left: 1px solid #e7e7e7;
        .detail-wrap {
          margin-right: 0;
        }
      }
    }
    .footer-list {
      height: 80px;
      flex-shrink: 0;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      padding: 0 24px;
      .footer-btn {
        height: 32px;
        border: 1px solid #0087d6;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-left: 16px;
        padding: 0 15px;
        border-radius: 3px;
        font-size: 14px;
        cursor: pointer;
        transition: all 0.3s;
        &.white {
          background: #fff;
          color: #0087d6;
          &:hover {
            color: #4e52da;
            border: 1px solid #4e52da;
          }
        }
        &.blue {
          background: #0087d6;
          color: #fff;
          &:hover {
            background: #4e52da;
            border: 1px solid #4e52da;
          }
        }
      }
      &.has-scroll {
        border-top: 1px solid #e7e7e7;
      }
    }
  }
}
::v-deep.detail-top {
  .detail-left {
    .el-image-item {
      .image-slot {
        height: 100%;
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
}
</style>
